package FloodFill;

/**
 * @author: yangwk
 * @date: 2023/4/28
 * @description:
 **/
public class 矩形入口 {
    static boolean[][] visited = new boolean[100][100];

    static int fill2(int[][] image, int origColor, int newColor) {
        for (int i = 0; i < image.length; i++) {
            for (int j = 0; j < image[0].length; j++) {
                if (image[i][j] == origColor && find2(image, i, j)) {
                    image[i][j] = newColor;
                }
            }
        }

        return 1;
    }

    public static boolean find2(int[][] image, int x, int y) {
        if (!inArea(image, x, y)) {
            return true;
        }
        if (image[x][y] == 0 && (x == 0 || x == image.length - 1 || y == 0 || y == image[0].length - 1)) {
            return false;
        }
        if (visited[x][y]) {
            return true;
        }
        visited[x][y] = true;
        if (image[x][y] == 1) {
            return true;
        }
        return find2(image, x - 1, y) &
                find2(image, x, y - 1) &
                find2(image, x + 1, y) &
                find2(image, x, y + 1);
    }

    static boolean inArea(int[][] image, int x, int y) {
        return x >= 0 && x < image.length && y >= 0 && y < image[0].length;
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 1, 1},
                {1, 0, 1},
                {1, 1, 1}};
        fill2(arr, 0, 1);
        System.out.println(arr);

    }
}
